#include "StdAfx.h"
#include "led.h"
#include "LedList.h"
#include "DialogPhoto.h"
#include "common.h"
#include "ledDlg.h"
#include <assert.h>

CLedList::CLedList(CDialogEx *dlg)
{
	m_dlg = dlg;
	m_beginIndex = 0;
}


CLedList::~CLedList(void)
{
}

void CLedList::setFocus(CDialogCommon *dlg)
{
	for(unsigned i = 0; i < m_list.size(); i++){
		m_list[i]->setSelected(m_list[i] == dlg);
	}
}

int CLedList::createItme(int type)
{
	CDialogCommon *dialog = CDialogCommon::create(m_dlg, m_list.size(), type);
	if(!dialog){
		return -1;
	}

	insert(dialog);
	return 0;
}

int CLedList::getIndex()
{
	for(unsigned i = 0; i < m_list.size(); i++){
		if(m_list[i]->getSelected())
			return i;
	}

	return -1;
}

void CLedList::moveDown()
{
	int	index = getIndex();
	if(0 > index || index+1 >= m_list.size())
		return;

	CDialogCommon *tmp = m_list[index];
	m_list[index] = m_list[index+1];
	m_list[index+1] = tmp;

	m_list[index]->setIndex(index);
	m_list[index+1]->setIndex(index+1);
}

void CLedList::insert(CDialogCommon *dlg)
{
	for(vector<CDialogCommon*>::iterator it = m_list.begin(); it != m_list.end(); it++){
		if((*it)->getSelected()){
			(*it)->setSelected(false);

			dlg->setSelected(true);
			dlg->setRange(m_beginIndex, m_beginIndex+canShowCount());
			
			it = m_list.insert(it+1, dlg);

			bool b = (it != m_list.end());

			for(; it != m_list.end(); it++){
				int index = it-m_list.begin();
				(*it)->setIndex(index);
			}

			((CledDlg*)m_dlg)->SetRangeMax(maxRange());
			return;
		}
	}

	assert(m_list.empty());
	m_list.push_back(dlg);
	dlg->setSelected(true);
	dlg->setIndex(0);
	return;

	int	index = getIndex();
	if(0 > index){
		assert(m_list.empty());

		dlg->setSelected(true);
		m_list.push_back(dlg);
		return;
	}

	m_list[index]->setSelected(false);
	index++;

	m_list.push_back(dlg);
	for(unsigned i = index; i+1 < m_list.size(); i++){
		m_list[i+1] = m_list[i];
		m_list[i+1]->setIndex(i+1);
	}
		
	m_list[index] = dlg;
	m_list[index]->setIndex(index);
	m_list[index]->setSelected(true);
}

void CLedList::moveUp()
{
	int	index = getIndex();
	if(0 >= index || index >= m_list.size())
		return;

	CDialogCommon *tmp = m_list[index];
	m_list[index] = m_list[index-1];
	m_list[index-1] = tmp;

	m_list[index]->setIndex(index);
	m_list[index-1]->setIndex(index-1);
}

void CLedList::remove()
{
	for(vector<CDialogCommon*>::iterator it = m_list.begin(); it != m_list.end(); it++){
		if((*it)->getSelected()){
			CDialogCommon *dialog = *it;
			it = m_list.erase(it);
			vector<CDialogCommon*>::iterator itend = m_list.end();
			delete dialog;
			if(it != m_list.end()){
				(*it)->setSelected(true);
			}else{
				if(!m_list.empty()){
					it--;
					(*it)->setSelected(true);
				}
			}

			for(; it != m_list.end(); it++){
				int index = it-m_list.begin();
				(*it)->setIndex(index);
			}

			((CledDlg*)m_dlg)->SetRangeMax(maxRange());
			return;
		}
	}

	return;

	int	index = getIndex();
	if(0 > index)
		return;

	delete m_list[index];
	if(m_list.size() > index+1){
		m_list[index+1]->setSelected(true);
	}else if(index > 0){
		m_list[index-1]->setSelected(true);
	}

	for(unsigned i = index; i+1 < m_list.size(); i++){
		m_list[i] = m_list[i+1];
		m_list[i]->setIndex(i);
	}

	m_list.pop_back();
}


unsigned CLedList::canShowCount()
{
	RECT rect;

	m_dlg->GetDlgItem(IDC_STATIC_LIST)->GetWindowRect(&rect);

	return ((rect.bottom-rect.top) - DIALOG_TOP_OFFSET) / CDialogCommon::height();
}

unsigned CLedList::maxRange()
{
	int max = m_list.size() - canShowCount();

	return max > 0 ? max : 0;
}

void CLedList::setbeginIndex(unsigned begin)
{
	m_beginIndex = begin;

	for(unsigned i = 0; i < m_list.size(); i++){
		m_list[i]->setRange(m_beginIndex, m_beginIndex+canShowCount());;
	}
}

int CLedList::write(class ledImage *led)
{
	int ret = 0;

	int count = 0;
	for(unsigned i = 0; i < m_list.size(); i++){
		int err = m_list[i]->count();
		count += err;
	}

	for(unsigned i = 0; i < m_list.size(); i++){
		int err = m_list[i]->write(led, count, ret);
		if(0 > err)
			return -1;

		ret += err;
	}

	return ret;
}